﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using FL.Bussiness;
using FL.DBModule.Sys;
using FL.DBModule;
using FL.SysEntity;

namespace FL.CodeGenerator
{
    public class Generator
    {
        public static IList<v_sys_objects> getTables()
        {
            BaseBussnies bll = new BaseBussnies();
            TableInfoStruc t = new TableInfoStruc();
            IList<v_sys_objects> list = bll.GetEntitys<v_sys_objects>();
            return list;
        }

        public static string GeneratorUI(string tableName,string storePath)
        {
            BaseBussnies bll = new BaseBussnies();
            TableInfoStruc t = new TableInfoStruc();
            IList<TableInfoStruc> list = bll.GetEntitys<TableInfoStruc>("TableName='" + tableName + "'");
            StringBuilder html = new StringBuilder("");
            html.Append(" <table style=\"width: 100%;\">");
            if (list != null)
            {
                foreach (TableInfoStruc table in list)
                {
                    string template = @" <tr>
                    <td class=""theader"">{0}</td>
                    <td>
                        <input id=""{1}"" name=""{1}"" allowinput=""true"" shownullitem=""false"" class=""mini-{2}"" style=""width: 100%;""/>
                    </td>
                </tr>";
                    html.AppendLine(string.Format(template, table.ColDesc, table.ColName, ConvertType(table.CType).Split(':')[1]));
                }

            }
            html.Append("</table>");
            return html.ToString();
        }

        public static string  GeneratorCode(string tableName,string storePath,string space)
        {
            BaseBussnies bll = new BaseBussnies();
            TableInfoStruc t = new TableInfoStruc();
            IList<TableInfoStruc> list = bll.GetEntitys<TableInfoStruc>("TableName='" + tableName + "'");
            TableInfoStruc entity = list.Where(p => p.PKey.Trim() == "Y").First();
            StringBuilder html = new StringBuilder("");
            html.Append(
                @"using System;
                using System.Collections.Generic;
                using System.Linq;
                using System.Text;
                using System.Threading.Tasks;
                using FL.DBModule;
                using FL.Lite;");
            html.AppendLine();
            html.AppendLine(@"namespace " + space + @"
            {
                [Serializable]
                [FL.DBModule.Table(""" + tableName +string.Format( @""", ""{0}"")]
                public class ",(entity==null?entity.ColName:"id")) + tableName + @" : BaseEntity
                {");
            if (list != null)
            {
                foreach (TableInfoStruc table in list)
                {
                    string template = @"        //"+table.ColDesc+@" 
                    [Column]
                    public "+ ConvertType(table.CType).Split(':')[0]+@" "+table.ColName+@"
                    { get; set; }";
                    html.AppendLine(template);
                }

            }
            html.AppendLine(@"
   }
}");
            return html.ToString();    
        }

        static string ConvertType(string dataType)
        {
            switch (dataType.ToLower())
            {
                case "int":
                    return "int:textbox";
                case "smallint":
                    return "int:textbox";
                case "datetime":
                    return "DateTime:datepicker";
                case "varchar":
                    return "string:textbox";
                case "uniqueidentifier":
                    return "Guid:combobox";
                default:
                    return "string:textbox";

            }
        }
    }
}
